﻿using DevExpress.XtraBars;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;
using SilkeyERP.Model;

namespace SilkeyERP.UI.Entities.Cities
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<City> _controller = new CityController();
        public override string ModuleName { get { return "Cities"; } }      

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);            
            MapItLoad(_controller);
            provinceBindingSource.DataSource = new ProvinceController().GetAll();
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

        private void gvBaseContentDetail_RowUpdated(object sender, RowObjectEventArgs e)
        {
            MapItUpdate(e, _controller);
        }

        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }

        private void gvBaseContentDetail_ValidateRow(object sender, ValidateRowEventArgs e)
        {

            var view = sender as GridView;
            var descriptionCol = view.Columns["Description"];
            var provinceIdCol = view.Columns["ProvinceID"];


            if (string.IsNullOrEmpty(((City)e.Row).Description))
            {
                e.ErrorText = "Descripcion es Requerido.";
                e.Valid = false;
                view.SetColumnError(descriptionCol, "Descripcion es Requerido.");
            }

            if (((City)e.Row).ProvinceID == int.MinValue)
            {
                e.ErrorText = "Provincia es Requerido.";
                e.Valid = false;
                view.SetColumnError(provinceIdCol, "Provincia es Requerido.");
            }
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new CityController());
        }           
    }
}
